# Maintainer: Anatol Pomozov

pkgname=osquery
pkgver=5.7.0
pkgrel=1
pkgdesc='SQL powered operating system instrumentation, monitoring, and analytics'
arch=(loong64 x86_64)
url='https://osquery.io'
license=(Apache GPL2)
makedepends=(cmake ninja clang python gcc-libs git libunwind)
source=(git+https://github.com/osquery/osquery.git#tag=$pkgver
        fixes.patch
	rocksdb.patch
        ebpf-common.patch
        libaudit.patch)
sha256sums=('SKIP'
            'b8a570a39a397e05237eeee950adba9fc67ff20b268b25818bbdfd6e6b616e64'
            'f68fea00796db328edca1bc5e7a437522779220414f40c03708dc2ff9201cc8e'
            '88cba2eebd12079bf3a4a8c5ba4ed34b1b5ccd9f9ac3fa518f1591396176666d'
            '96218ef5b7d6d6deb3a7b4b3dfed8068b7e4d10acd5b19372b9882f89d4478a8')

prepare() {
  cd osquery

  # the patches come from https://github.com/anatol/osquery
  patch -p1 < ../fixes.patch
  patch -p1 < ../rocksdb.patch
  # git does not pick .gitmodules changes for some reason. add the submodule explicitly.
  #mkdir libraries/cmake/source/snappy/src
  git submodule add https://github.com/google/snappy libraries/cmake/source/snappy/src

  git submodule update --init --recursive

  (cd $srcdir/osquery/libraries/cmake/source/libaudit/src && patch -p1 < $srcdir/libaudit.patch)
  (cd $srcdir/osquery/libraries/cmake/source/ebpfpub/src/ && git submodule update --init && cd libraries/ebpf-common/src/ && patch -p1 < $srcdir/ebpf-common.patch)
}

build() {
  cd osquery

  CC=clang CXX=clang++ cmake \
      -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr -DOSQUERY_VERSION=$pkgver \
      -G Ninja -S . -B build

  ninja -C build
}

package() {
  cd osquery

  DESTDIR="${pkgdir}" ninja -C build install
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  install -Dm644 tools/deployment/osquery.example.conf "$pkgdir/etc/osquery/osquery.conf"
  install -Dm644 tools/deployment/linux_packaging/osqueryd.sysconfig "$pkgdir/etc/sysconfig/osqueryd"
  install -Dm644 tools/deployment/linux_packaging/rpm/osqueryd.service "$pkgdir/usr/lib/systemd/system/osqueryd.service"
  rm -r $pkgdir/control
}
